package com.guo.entity.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guo.entity.Movie;
import com.guo.mapper.MovieMapper;
import com.guo.query.MovieQuery;
import com.guo.entity.service.IMovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class MovieServiceImpl implements IMovieService {

    @Autowired
    private MovieMapper movieMapper;

    @Override
    public List<Movie> getAll() {
        return movieMapper.queryAll();
    }

    @Override
    public Movie getById(Integer id) {
        return movieMapper.queryById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateById(Movie movie) {
        return movieMapper.updateById(movie);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int removeById(Integer id) {
        return movieMapper.deleteById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int addById(Movie movie) {
        return movieMapper.insert(movie);
    }

    @Override
    public PageInfo<Movie> pageAll(MovieQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Movie> all  = movieMapper.pageAll(query);
        Page<Movie> page = (Page<Movie>) all;
        PageInfo<Movie> info = page.toPageInfo();
        return info;
    }
}
